<?php


require_once(DIR_LIB."VsCore".DIR_SEP."db.php");

require_once(DIR_LIB."VsCore".DIR_SEP."vsauth.php");
require_once(DIR_LIB."VsCore".DIR_SEP."vsauth_data.php");

class VsUser {

	protected $_sqlTable = 'users';

	protected $_id = NULL;	
	protected $_authId = NULL;

	// Auth Information
	protected $_authData = NULL; // AuthData Object Or NULL for Anonymous


	// Personal Data
	protected $_userTitle = '';	// Mr. M-s.
	protected $_userFirstName = '';
	protected $_userSecondName = '';
	protected $_userFamilyName = '';

	//Contact Data
	protected $_telephone = '';


	static function makeUsers($sqlTableAuth, $sqlTable) {
		$listUsers = array();
		$auth = VsAuth::Instance();

		//echo "Auth Id: ".$auth->id()."<br>";
		//echo "Auth Level: ".$auth->level()."<br>";
		//echo "Auth Name: ".$auth->name()."<br>";

		if($auth->level()<=5) {
			array_push($listUsers, makeUserById($auth->id(), $sqlTableAuth, $sqlTable));

			return $listUsers;
		}

		$db = VsDatabase::Instance();
		$q = "SELECT * FROM `".$sqlTable."`";

		$res = $db->query($q);
		while($rUser = @mysql_fetch_object($res)) {

			$authData = VsAuthData::makeUserById($rUser->auth_id, $sqlTableAuth);
			//$authData = VsAuthData::makeUserById($rUser->id, $sqlTableAuth);
			if($authData->level() > $auth->level())
				continue;
			
			$user = new VsUser($sqlTable);
			$user->_authData = $authData;

			$user->_id = $rUser->auth_id;

			// Personal Data
			$user->_userTitle = $rUser->title;
			$user->_userFirstName = $rUser->name;
			$user->_userSecondName = $rUser->second_name;
			$user->_userFamilyName = $rUser->family;

			// Contact Data
			//$user->_userMail = $rUser->email;
			
			array_push($listUsers, $user);
		}

		return $listUsers;
	}
	//=========================================================================================

	static function makeUserFromForm($form, $sqlTableAuth, $sqlTable) {
		$user = new VsUser($sqlTable);

		$auth = VsAuth::Instance();
		if(($auth->level()<=5) && $auth->id()!=$id)
			return $user;


		$user->_authData = VsAuthData::makeUserFromForm($form, $sqlTableAuth);

		$user->_id = $form['user_id'];

		// Personal Data
		$user->_userTitle =  $form['title'];
		$user->_userFirstName =  $form['firstname'];
		$user->_userSecondName =  isset($form['secondname']) ? $form['secondname'] : '';
		$user->_userFamilyName =  $form['lastname'];

		// Contact Data
		$user->_userMail =  $form['email'];

		return $user;
	}
	//=========================================================================================

	static function makeUserById($id, $sqlTableAuth, $sqlTable) {
		$user = new VsUser($sqlTable);

		$auth = VsAuth::Instance();
		if(($auth->level()<=5) && $auth->id()!=$id)
			return $user;

		$db = VsDatabase::Instance();
		//$q = "SELECT * FROM `".$sqlTable."` WHERE id=".$id;
		$q = "SELECT * FROM `".$sqlTable."` WHERE auth_id=".$id;
		$res = $db->query($q);

		$rUser = @mysql_fetch_object($res);
		if($rUser){
			if($sqlTableAuth === $sqlTable)
				$authData = VsAuthData::makeUserById($rUser->id, $sqlTableAuth);
			else
				$authData = VsAuthData::makeUserById($rUser->auth_id, $sqlTableAuth);

			$user->_authData = $authData;

			//$user->_id = $rUser->id;
			$user->_id = $rUser->auth_id;

			// Personal Data
			$user->_userTitle = $rUser->title;
			$user->_userFirstName = $rUser->name;
			$user->_userSecondName = $rUser->second_name;
			$user->_userFamilyName = $rUser->family;

			// Contact Data
			//$user->_userMail = $rUser->email;
		}

		return $user;
	}
	//=========================================================================================

	protected function __construct($sqlTable) {
		$this->_sqlTable = $sqlTable;
	}
	//=========================================================================================

	function save() {
		if($this->_id > 0)
			$this->update();
		else
			$this->insert();
	}

	// AuthData Methods
	function authName() { return $this->_authData ? $this->_authData->name() : 'Anonymous'; }
	function authPassword() { return $this->_authData ? $this->_authData->password() : 0; }
	function authLevel() { return $this->_authData ? $this->_authData->level() : 0; }
	function level() { return $this->authLevel(); }
	function authEmail() { return $this->_authData ? $this->_authData->email() : ''; }

	function lastLoginTime() { return $this->_authData->lastLogin(); }
	function lastLoginHost() { return $this->_authData->lastLoginHost(); }

	function phone() { return 'n/a'; }
	function mail() { return $this->authEmail(); }
	//=========================================================================================

	function id() { return $this->_id; }
	function authId() { return $this->_authId; }

	function getTitle() { return $this->_userTitle; }
	function title() { return $this->_userTitle; }
	function getFirstName() { return $this->_userFirstName; }
	function firstName() { return $this->_userFirstName; }
	function getSecondName() { return $this->_userSecondName; }
	function secondName() { return $this->_userSecondName; }

	function getFamilyName() { return $this->_userFamilyName; }
	function familyName() { return $this->_userFamilyName; }
	function lastName() { return $this->_userFamilyName; }

	function getMail() { return $this->_userMail; }
	//function mail() { return $this->_userMail; }

	function setTitle($title) { $this->_userTitle = $title; }
	function setFirstName($name) { $this->_userFirstName = $name; }
	function setSecondName($name) { $this->_userSecondName = $name; }
	function setFamilyName($name) { $this->_userFamilyName = $name; }
	function setMail($mail) { $this->_userMail = $mail; }

	private function insert() {
		$auth = VsAuth::Instance();
		if(($auth->level()<=5) && $auth->id()!=$this->_authId)
			return;

		$this->_authId = $this->_authData->save();

		$q = "INSERT INTO `".$this->_sqlTable."` (`auth_id`, `title`, `name`, `second_name`, `family`, `phone`, `date_registered`) VALUES('".$this->_authId."', '".$this->_userTitle."', '".$this->_userFirstName."', '".$this->_userSecondName."', '".$this->_userFamilyName."', '".$this->_telephone."', NOW())";
		//echo $q;

		$db = VsDatabase::Instance();
		$db->query($q);
		//echo mysql_error();

		$this->_id = mysql_insert_id();
	}
	//=======================================================================================================================

	private function update() {
		$auth = VsAuth::Instance();
		if(($auth->level()<=5) && $auth->id()!=$this->_authId)
			return;

		$q = "UPDATE `".$this->_sqlTable."` SET `products_categories_id`='".$this->_categoryId."', `name`='".addslashes($this->_name)."', `info`='".addslashes($this->_info)."', date=NOW() WHERE id=".$this->_id;

		$db = VsDatabase::Instance();
		$db->query($q);

	}
	//=======================================================================================================================

};

?>
